//
//  SHTabBarViewController.m
//  ShinyOrder
//
//  Created by 王方伟 on 16/7/22.
//  Copyright © 2016年 ShinyHub. All rights reserved.
//

#import "SHTabBarViewController.h"
#import "SHNavigationController.h"
#import "SHTabBarView.h"

@interface SHTabBarViewController ()<TabBarViewDelegage>
@property (nonatomic, weak) SHTabBarView *customTabBar;
@end

@implementation SHTabBarViewController
    
+ (instancetype)shareInstance{
    static SHTabBarViewController *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[SHTabBarViewController alloc] init];
    });
    return shareInstance;
}

+ (instancetype)tabBarControllerWithAddChildVCsBlock:(void (^)(SHTabBarViewController *))addVCBlock{
    SHTabBarViewController *tabBarVC = [[SHTabBarViewController alloc] init];
    if (addVCBlock) {
        addVCBlock(tabBarVC);
    }
    return tabBarVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加自定义TabBar
    [self initTabBar];
}

- (void)setIsAddCenterButton:(BOOL)isAddCenterButton{
    self.customTabBar.isAddCenterButton = isAddCenterButton;
}
    
    
#pragma mark 删除默认的TabBar
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    for (UIView *childView in self.tabBar.subviews) {
        if ([childView isKindOfClass:[UIControl class]]) {
            [childView removeFromSuperview];
        }
    }
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
    for (UIView *childView in self.tabBar.subviews) {
        if ([childView isKindOfClass:[UIControl class]]) {
            [childView removeFromSuperview];
        }
    }
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    _customTabBar.frame=self.tabBar.bounds;
    
}

#pragma mark 自定义TabBar
-(void)initTabBar{
    SHTabBarView *customTabBar=[[SHTabBarView alloc]init];
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
    //设置代理
    self.customTabBar.delegate=self;
}

#pragma mark TabBar的代理切换控制器
-(void)tabBar:(SHTabBarView *)tabBar didSelectedButtonFrom:(long)from to:(long)to{
    self.selectedIndex=to;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SelectedTabBarItemNotification" object:nil userInfo:nil];
}


- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    _customTabBar.selectedIndex = selectedIndex;
}



#pragma mark 创建一个子控件
-(void)addChickViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName isNavController:(BOOL)isNav{
    //设置子控制器的属性
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage=[[UIImage imageNamed:selectImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    if (isNav) {
        //将子控制器添加到导航控制器中
        SHNavigationController *nc = [[SHNavigationController alloc]initWithRootViewController:childVc];
        [self addChildViewController:nc];
    }else{
        [self addChildViewController:childVc];
    }
    [self.customTabBar addTabBarButtonWith:childVc.tabBarItem];
}

@end
